package com.java78.addon.journal.directive;

import com.java78.addon.journal.model.JpressAddonJournal;
import com.java78.addon.journal.service.JpressAddonJournalService;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.template.Env;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;

import io.jboot.db.model.Columns;
import io.jboot.web.controller.JbootControllerContext;
import io.jboot.web.directive.JbootPaginateDirective;
import io.jboot.web.directive.annotation.JFinalDirective;
import io.jboot.web.directive.base.JbootDirectiveBase;
import io.jpress.commons.utils.MarkdownUtils;

/**
 * @description: 查询日志
 * @author: linsan
 * @create: 2020-03-21 12:51
 **/
@JFinalDirective("JournalList")
public class JournalListDirective extends JbootDirectiveBase {

    @Inject
    private JpressAddonJournalService service;
    
    @Override
    public void onRender(Env env, Scope scope, Writer writer) {
    	Controller controller = JbootControllerContext.get();
    	int page = controller.getAttr("page");
        int pageSize = getParaToInt("pageSize", scope, 10);
    	String orderBy = getPara("orderBy", scope,"created desc");//获取到orderBy
        Columns c = new Columns();
        c.eq("status",1);
        Page<JpressAddonJournal> journalPages = service.paginateByColumns(page, pageSize, c, orderBy);
        if(journalPages != null ) {
        	for(JpressAddonJournal journal : journalPages.getList()) {
        		journal.setContent(MarkdownUtils.toHtml(journal.getContent()));
        	}
        	scope.setGlobal("journalPages", journalPages);
        }
        renderBody(env, scope, writer);
    }

    @Override
    public boolean hasEnd() {
        return true;
    }
    
    @JFinalDirective("journalPaginate")
    public static class TemplatePaginateDirective extends JbootPaginateDirective {
    	
    	@Override
        protected Page<?> getPage(Env env, Scope scope, Writer writer) {
            return (Page<?>) scope.get("journalPages");
        }

    }
}
